package cn.stu.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stu.cms.dto.PageDto;
import cn.stu.cms.mapper.MenuMapper;
import cn.stu.cms.pojo.Menu;
import cn.stu.cms.service.MenuService;
import cn.stu.cms.util.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static cn.stu.cms.common.CacheConstants.ROUTER_CACHE;
import static cn.stu.cms.common.CommonConstants.*;


/**
 * 菜单服务实施
 *
 * @author gh
 * @date 2023/09/11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    /**
     * 菜单列表
     *
     * @param page  页面
     * @param title 标题
     * @param pid   PID
     * @return {@link R}<{@link PageDto}<{@link Menu}>>
     */
    @Override
    public R<PageDto<Menu>> menuList(PageDto<Menu> page, String title, Long pid) {
        lambdaQuery().like(StrUtil.isNotBlank(title), Menu::getTitle, title)
                .eq(pid != null, Menu::getParentId, pid)
                .or()
                .eq(pid != null, Menu::getMenuId, pid)
                .page(page);
        return R.ok(page, "获取成功");
    }

    /**
     * 菜单添加
     *
     * @param add 添加
     * @return {@link R}
     */
    @Override
    @CacheEvict(value = ROUTER_CACHE, allEntries = true, beforeInvocation = true)
    public R<String> menuAdd(Menu add) {
        if (add.getParentId() != 0) {
            boolean exists = baseMapper.exists(lambdaQuery().eq(Menu::getMenuId, add.getParentId()).getWrapper());
            if (!exists) {
                return R.failed(ID_ERROR);
            }
        }
        baseMapper.insert(add);
        return R.ok(SAVE_OK_MSG);
    }

    /**
     * 菜单更新
     *
     * @param update 更新
     * @return {@link R}
     */
    @Override
    @CacheEvict(value = ROUTER_CACHE, allEntries = true, beforeInvocation = true)
    public R<String> menuUpdate(Menu update) {
        if (update.getMenuId() == null) {
            return R.failed(ID_ERROR);
        }
        if (Objects.equals(update.getMenuId(), update.getParentId())) {
            return R.failed("不能成为自己的子菜单！");
        }
        if (update.getParentId() != 0) {
            boolean exists = lambdaQuery().eq(Menu::getMenuId, update.getParentId()).exists();
            if (!exists) {
                return R.failed(EDIT_FAIL_MSG);
            }
        }
        if (update.getParentId() != 1) {
            if (StrUtil.isBlank(update.getPath())) {
                return R.failed(EDIT_FAIL_MSG);
            }
        }
        baseMapper.updateById(update);
        return R.ok(EDIT_OK_MSG);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return {@link R}
     */
    @Override
    @CacheEvict(value = ROUTER_CACHE, allEntries = true, beforeInvocation = true)
    public R<String> menuDel(Long menuId) {
        boolean menuExists = lambdaQuery().eq(Menu::getMenuId, menuId).exists();
        if (!menuExists) {
            return R.failed(ID_ERROR);
        }
        boolean childExists = lambdaQuery().eq(Menu::getParentId, menuId).exists();
        if (childExists) {
            return R.failed("请先删除子菜单");
        }
        baseMapper.deleteById(menuId);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 菜单信息
     *
     * @param menuId 菜单ID
     * @return {@link R}
     */
    @Override
    public R<Menu> menuInfo(Long menuId) {
        return R.ok(baseMapper.selectById(menuId), GET_INFO_OK);
    }
}
